package com.sinosoft.cnmuseum.servlets;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sinosoft.cnmuseum.dao.MbMusicDbrwDao;
import com.sinosoft.cnmuseum.entity.MbMusicDbrw;
import com.sinosoft.cnmuseum.utils.Status;
import com.sinosoft.cnmuseum.utils.StringUtils;
import com.sinosoft.commontools.annotated.TransAnnotation;
import com.sinosoft.commontools.annotated.rquestMethod;
import com.sinosoft.commontools.annotated.rquestMethod.ReqMode;
import com.sinosoft.commontools.record.Record;

public class MbDbrwServlet {

  @rquestMethod(MethodNote = ReqMode.All)
  @TransAnnotation(isTrans=true)
  public Record getDbrwListByGcxxId(HttpServletRequest request, HttpServletResponse response, Record data){
    Record result = new Record();
    String gcxxid = data.getStrValue("gcxxid");
    if(StringUtils.isEmpty(gcxxid))
      result.add(Status.SATUS, Status.FAILED);
    else{
      List list = getDbrwListByGcxxId(Integer.valueOf(gcxxid));
      result.add(Status.SATUS, Status.OK);
      result.add(Status.ROWS, list);
    }
    return result;
    
  }

  public static List<Record> getDbrwListByGcxxId(int gcxxid) {
    String sql = "select * from "+MbMusicDbrw.MB_MUSIC_DBRW_TABLE_NAME +" where gcxxid="+gcxxid;
    MbMusicDbrwDao dao = new MbMusicDbrwDao();
    List<Record> list = dao.getBySql(sql);
    return list;    
  }
  
  @rquestMethod(MethodNote = ReqMode.All)
  @TransAnnotation(isTrans=true)
  public Record getDbrwDetailByZdbh(HttpServletRequest request, HttpServletResponse response, Record data){
    Record result = new Record();
    String zdbhStr = data.getStrValue("zdbh");
    if(StringUtils.isEmpty(zdbhStr))
      result.add(Status.SATUS, Status.FAILED);
    else{
      MbMusicDbrwDao dao = new MbMusicDbrwDao();
      Record dbrw = dao.getById(Integer.valueOf(zdbhStr));
      result.add(Status.SATUS, Status.OK);
      result.add(Status.ROWS,dbrw);
    }
    return result;
  }
  

  @rquestMethod(MethodNote = ReqMode.All)
  @TransAnnotation(isTrans=true)
  public Record saveDbrw(HttpServletRequest request, HttpServletResponse response, Record data){
    Record result = new Record();
    MbMusicDbrwDao dao = new MbMusicDbrwDao();
    String zdbhStr = data.getStrValue("dbrw_zdbh");
    if(StringUtils.isEmpty(zdbhStr)){
      Record r = new Record();
      r.add("name", data.getStrValue("name"));
      r.add("dbrwYear", data.getStrValue("dbrwYear"));
      r.add("contribution", data.getStrValue("contribution"));
      r.add("representativeworks", data.getStrValue("representativeworks"));
      r.add("gcxxid", data.getStrValue("gcxxid"));
      dao.insert(r);
      result.add(Status.SATUS, Status.OK);
    }
    else{
      Record r = dao.getById(Integer.valueOf(zdbhStr));
      r.add("name", data.getStrValue("name"));
      r.add("dbrwYear", data.getStrValue("dbrwYear"));
      r.add("contribution", data.getStrValue("contribution"));
      r.add("representativeworks", data.getStrValue("representativeworks"));
      r.add("gcxxid", data.getStrValue("gcxxid"));
      dao.update(r);
      result.add(Status.SATUS, Status.OK);
    }
    return result;
  }
  

  @rquestMethod(MethodNote = ReqMode.All)
  @TransAnnotation(isTrans=true)
  public Record delDbrw(HttpServletRequest request, HttpServletResponse response, Record data){
    Record result = new Record();
    MbMusicDbrwDao dao = new MbMusicDbrwDao();
    String zdbhStr = data.getStrValue("zdbh");
    if(StringUtils.isEmpty(zdbhStr)){
      result.add(Status.SATUS, Status.FAILED);
    }else{
      dao.delete(dao.getById(Integer.valueOf(zdbhStr)));
    }
    return result;
  }
}
